<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../template/AdminTemplate.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:define name="title">
        District management
    </ui:define>

    <ui:define name="content">
        <center>
        <h:form id="form">
            
            <p:growl id="growl1" showDetail="true" life="3000" />
            <h:panelGrid columns="1">

                <h:panelGrid columns="1">

                    <p:dataTable var="districtvar" value="#{DistrictMBean.districtList}" rowKey="#{districtvar.districtID}"
                                 selection="#{DistrictMBean.districtedit}" selectionMode="single" filteredValue="#{DistrictMBean.filteredDistrict}" id="districtTable"
                                 paginator="true" rows="10"  
                                 paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown}"
                                 rowsPerPageTemplate="5,10,15">

                        <p:ajax event="rowSelect" update=":form:display" oncomplete="districtDialog.show()" />

                        <f:facet name="header">
                            District Management
                        </f:facet>

                        <p:column headerText="District ID" sortBy="#{districtvar.districtID}" filterBy="#{districtvar.districtID}">
                            #{districtvar.districtID}
                        </p:column>

                        <p:column headerText="District Name" sortBy="#{districtvar.districtName}" filterBy="#{districtvar.districtName}">
                            #{districtvar.districtName}
                        </p:column>
                        
                        <p:column headerText="City" sortBy="#{districtvar.cityID}" filterBy="#{districtvar.cityID.cityName}">
                            #{DistrictMBean.selectCityNameByID(districtvar.cityID)}
                        </p:column>

                    </p:dataTable>

                    <p:dialog header="District Detail" widgetVar="districtDialog" resizable="false"  
                              width="350" showEffect="explode" hideEffect="explode">

                        <h:panelGrid id="display" columns="2" cellpadding="4">

                            <h:outputText value="District ID " />
                            <h:inputText value="#{DistrictMBean.districtedit.districtID}" id="districtID"/>                        

                            <h:outputText value="District Name" />
                            <h:inputText value="#{DistrictMBean.districtedit.districtName}" id="districtName"/>

                            <h:outputText value="City" />
                            <p:selectOneMenu value="#{DistrictMBean.cityID}">  
                                <c:if test="#{!empty DistrictMBean.districtedit.cityID}">
                                    <f:selectItem itemLabel="#{DistrictMBean.selectCityNameByID(DistrictMBean.districtedit.cityID)}" itemValue="#{DistrictMBean.districtedit.cityID.cityID}" />  
                                </c:if>
                                <f:selectItems value="#{DistrictMBean.cityList2}" var="item" itemLabel="#{item.cityName}" itemValue="#{item.cityID}"/>  
                            </p:selectOneMenu>
                            
                            <p:commandButton value="Delete" action="#{DistrictMBean.removeDistrict(DistrictMBean.districtedit.districtID)}" oncomplete="districtDialog.hide()" update=":form:districtTable, :form:growl1"/>
                            <p:commandButton value="Update" action="#{DistrictMBean.editDistrict(DistrictMBean.districtedit)}" oncomplete="districtDialog.hide()" update=":form:districtTable, :form:growl1"/>

                        </h:panelGrid>
                    </p:dialog>    

                </h:panelGrid>

            </h:panelGrid>

        </h:form>
        
        <h:form>
            <h:panelGrid columns="1">

                    <p:commandButton id="viewButton" value="Add new District" 
                                     oncomplete="addDistrictDialog.show()"/> 

                    <p:dialog id="dialogadd" header="Add New District" widgetVar="addDistrictDialog" resizable="false"  
                              showEffect="fade" hideEffect="fade">  
                        <h:panelGrid id="displayAdd" columns="2" cellpadding="4">  
                            <h:outputText value="District ID"/>
                            <p:inputText id="diID" value="#{DistrictMBean.district.districtID}" title="District ID" />                    

                            <h:outputText value="District Name" />
                            <p:inputText id="diName" value="#{DistrictMBean.district.districtName}" title="District Name"/> 

                            <h:outputText value="City" />
                            <p:selectOneMenu id="ciName" value="#{DistrictMBean.cityID}">  
                                <f:selectItem itemLabel="Select city" itemValue="" />  
                                <f:selectItems value="#{DistrictMBean.cityList}" var="item" itemLabel="#{item.cityName}" itemValue="#{item.cityID}"/>  
                            </p:selectOneMenu>
                        </h:panelGrid>  
                        <p:commandButton value="Create" action="#{DistrictMBean.createDistrict()}" oncomplete="addDistrictDialog.hide()" update=":form:districtTable, :form:growl1"/>                      
                    </p:dialog>

                </h:panelGrid>
        
        </h:form>
        </center>
    </ui:define>

</ui:composition>
